<?php if ( ! defined('BASEPATH')) exit('This file does not run directly. Sorry.'); ?>
<?php
/**
 * Kalaisoo.
 *
 * A lightweight no-framework framework written in PHP.
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 * @version $Id: event.php 146 2012-01-26 08:38:18Z info@sah-company.com $
 */

/**
 * Load dependencies.
 */
Registry::instance()->loader->model(array(
	'attendee'
	)
);

/**
 * Kalaisoo Todo Model Class.
 *
 * @package Kalaisoo
 * @subpackage Model
 * @author $Author: info@sah-company.com $
 */
class Model_Event extends Model
{
	/**
	 * default filter
	 *
	 * @var string
	 */
	public $filter = 'ahead';
	
	/**
	 * filters
	 *
	 * @var array
	 */
	public $filters = array();

	/**
	 * __construct.
	 */
	public function construct()
	{
		parent::__construct();
	}
	
	/**
	 * returns the tags of a bean.
	 *
	 * @return array
	 */
	public function getTags() {
		return array(
			$this->bean->summary
		);
	}
	
	/**
	 * loads the array $this->gestalt of this bean for use with Model::gestalt().
	 */
	public function loadGestalten()
	{
		$this->gestalt['default'] = array(
			array(
				'type' => 'text',
				'name' => 'start_date',
				'parameters' => array(
				)
			),
			array(
				'type' => 'text',
				'name' => 'end_date',
				'parameters' => array(
				)
			),
			array(
				'type' => 'checkbox',
				'name' => 'allday',
				'parameters' => array(
					'value' => 1
				)
			),
			array(
				'type' => 'select',
				'name' => 'recurrence',
				'help' => __('event_recurrence_help'),
				'parameters' => array(
					'options' => array(
						'none' => __('event_no_recurrence'),
						'daily' => __('event_daily_recurrence'),
						'weekly' => __('event_weekly_recurrence'),
						'biweekly' => __('event_biweekly_recurrence'),
						'monthly' => __('event_monthly_recurrence'),
						'quarterly' => __('event_quarterly_recurrence'),
						'yearly' => __('event_yearly_recurrence')
					)
				)
			),
			array(
				'type' => 'select',
				'name' => 'status',
				'parameters' => array(
					'options' => array(
						'none' => __('event_no_status'),
						'cancelled' => __('event_cancelled_status'),
						'confirmed' => __('event_confirmed_status'),
						'tentative' => __('event_tentative_status')
					)
				)
			),
			array(
				'type' => 'text',
				'name' => 'summary',
				'parameters' => array(
					'placeholder' => __('event_summary_placeholder'),
					'required' => 'required'
				)
			),
			array(
				'type' => 'textarea',
				'name' => 'description',
				'help' => __('event_description_help'),
				'placeholder' => __('event_description_placeholder'),
				'parameters' => array(
					'cols' => 60,
					'rows' => 8
				)
			),
			array(
				'type' => 'text',
				'name' => 'location',
				'parameters' => array(
					'placeholder' => __('event_location_placeholder'),
				)
			),
			array(
				'type' => 'url',
				'name' => 'url',
				'help' => __('event_url_help'),
				'parameters' => array(
					'placeholder' => __('event_url_placeholder'),
				)
			),
			array(
				'type' => 'own',
				'name' => 'attendee',
				'parameters' => array(
				)
			)
		);
	}
	
	/**
	 * validates the data and returns either true or false.
	 *
	 * @return bool
	 */
	public function validate()
	{
		$valid = true;
		return $valid;
	}

	/**
	 * open.
	 */
	public function open()
	{
	}
	
	/**
	 * returns the default order clause for sql queries
	 *
	 * @return array
	 */
	public function orderClause()
	{
		return 'event_start_date ASC, event_summary ASC';
		//return $this->order['field'].' '.$this->order['dir'];
	}

	/**
	 * dispense.
	 */
	public function dispense()
	{
		$this->bean->setMeta('buildcommand.unique', array(array('summary')));
		$this->filters = array(
			'ahead' => array(
				'label' => __('event_filter_ahead'),
				'where' => "event_end_date >= '".date('Y-m-d H:i:s')."'"
			),
			'past' => array(
				'label' => __('event_filter_past'),
				'where' => "event_end_date < '".date('Y-m-d H:i:s')."'"
			)
		);
	}

	/**
	 * update.
	 */
	public function update()
	{
		if ( ! $this->bean->getId()) {
			$now = time();
			if ( ! $this->bean->start_date) $this->bean->start_date = date('Y-m-d H:i:s', $now); // todate
			if ( ! $this->bean->end_date) $this->bean->end_date = date('Y-m-d H:i:s', strtotime($this->bean->start_date) + 3600); // todate
		}
	}
	
	/**
	 * after_update.
	 */
	public function after_update()
	{
	}
	
	/**
	 * delete.
	 */
	public function delete()
	{
	}
	
	/**
	 * after_delete.
	 */
	public function after_delete()
	{
	}
}
?>